package com.ele.client.activity.editor;

import com.ele.client.activity.editor.widget.IsEditorWidget;
import com.ele.client.activity.editor.widget.WordsPanel;
import com.ele.model.Question;
import com.ele.model.Word;
import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.ControlGroup;
import com.github.gwtbootstrap.client.ui.TextArea;
import com.github.gwtbootstrap.client.ui.TextBox;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import java.util.ArrayList;
import java.util.List;

public class EditorViewImpl extends Composite implements EditorView {

    interface DefaultUiBinder extends UiBinder<Widget, EditorViewImpl> {}
    private static DefaultUiBinder uiBinder = GWT.create(DefaultUiBinder.class);

    @UiField
    TextBox nameTextBox;
    @UiField
    TextArea descriptionTextArea;
    @UiField
    Button addQuestionButton;
    @UiField
    FlowPanel questionsPlaceholder;
    @UiField
    WordsPanel wordsPanel;
    @UiField
    Button addWordButton;
    @UiField
    Button saveButton;
    @UiField
    Button prevButton;
    @UiField
    Button nextButton;
    @UiField
    Button descriptionButton;
    @UiField
    ControlGroup descriptionControlGroup;

    private Presenter presenter;

    public EditorViewImpl() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void startLoading() {
        saveButton.state().loading();
    }

    @Override
    public void stopLoading() {
        saveButton.state().reset();
    }

    @Override
    public void setName(String name) {
        nameTextBox.setValue(name);
    }

    @Override
    public void setDescription(String description) {
        descriptionTextArea.setValue(description);
        boolean empty = description == null || description.isEmpty();
        setDescriptionVisible(!empty);
    }

    @Override
    public void clearQuestions() {
        questionsPlaceholder.clear();
    }

    @Override
    public void clearWords() {
        wordsPanel.clear();
    }

    @Override
    public void addQuestionControl(IsEditorWidget<Question> widget) {
        questionsPlaceholder.add(widget);
    }

    @Override
    public void addWordControl(IsEditorWidget<Word> widget) {
        wordsPanel.add(widget);
    }

    @Override
    public String getName() {
        return nameTextBox.getValue();
    }

    @Override
    public String getDescription() {
        return descriptionTextArea.getValue();
    }

    @Override
    public List<IsEditorWidget<Word>> getWordControls() {
        List<IsEditorWidget<Word>> widgets = new ArrayList<IsEditorWidget<Word>>();
        for (int i = 0; i < wordsPanel.getWidgetCount(); i++) {
            widgets.add((IsEditorWidget<Word>) wordsPanel.getWidget(i));
        }

        return widgets;
    }

    @Override
    public List<IsEditorWidget<Question>> getQuestionControls() {
        List<IsEditorWidget<Question>> widgets = new ArrayList<IsEditorWidget<Question>>();
        for (int i = 0; i < questionsPlaceholder.getWidgetCount(); i++) {
            widgets.add((IsEditorWidget<Question>) questionsPlaceholder.getWidget(i));
        }

        return widgets;
    }

    @Override
    public void removeWordControl(IsEditorWidget<Word> widget) {
        wordsPanel.remove(widget);
    }

    @Override
    public void removeQuestionControl(IsEditorWidget<Question> widget) {
        questionsPlaceholder.remove(widget);
    }

    @UiHandler("addWordButton")
    void onWordButtonClick(ClickEvent event) {
        presenter.addBlankWord();
    }

    @UiHandler("addQuestionButton")
    void onQuestionButtonClick(ClickEvent event) {
        presenter.addBlankQuestion();
    }

    @UiHandler("prevButton")
    void onPrev(ClickEvent event) {
        presenter.goToPrev();
    }

    @UiHandler("nextButton")
    void onNext(ClickEvent event) {
        presenter.goToNext();
    }

    @UiHandler("saveButton")
    void onSave(ClickEvent event) {
        presenter.save();
    }

    @UiHandler("descriptionButton")
    void onDescriptionButtonClick(ClickEvent event) {
        boolean visible = descriptionControlGroup.isVisible();
        setDescriptionVisible(!visible);
    }

    private void setDescriptionVisible(boolean visible) {
        descriptionControlGroup.setVisible(visible);
        descriptionButton.setText(visible ? "Hide Description" : "Show Description");
    }
}
